package bean.back.listpark;

import com.alibaba.fastjson.JSON;
import util.JDBCConnection;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BackListparkBean {
    int code;
    String message;
    int total;
    List<BackListparkItem>data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<BackListparkItem> getData() {
        return data;
    }

    public void setData(List<BackListparkItem> data) {
        this.data = data;
    }

    public String queryPark(String token){
        JDBCConnection jdbc = new JDBCConnection();
        String tokenArry[] = token.split("#");
        try{
            if(checkPermissions(tokenArry[0],tokenArry[2],jdbc)){
                String querySQL = "select lotid,lotname,lotstatus,lottel,lotaddress from parkinglot";
                ResultSet rs = jdbc.executeQuery(querySQL);
                data = new ArrayList<>();
                int cnt = 0;
                while (rs.next()){
                    BackListparkItem item = new BackListparkItem();
                    item.setId(rs.getString("lotid"));
                    item.setName(rs.getString("lotname"));
                    item.setStatus(rs.getString("lotstatus"));
                    item.setAddress(rs.getString("lotaddress"));
                    item.setTel(rs.getString("lottel"));
                    data.add(item);
                    cnt++;
                }
                total = cnt;
                code = 0;
                message = "成功";
            }
            else {
                code = 200;
                message = "token错误或者用户无权限";
            }
        }catch (Exception e){
            code = 100;
            message = "token错误";
        }
        return JSON.toJSONString(this);
    }

    private boolean checkPermissions(String operator_id, String operator_pwd, JDBCConnection jdbc){
        String querySQL = "select backpsd from backstage where backaccount = '"+operator_id+"'";
        ResultSet rs = jdbc.executeQuery(querySQL);
        try {
            rs.next();
            if(operator_pwd.equals(rs.getString("backpsd"))) return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
